package com.ss.android.common.app;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.ss.android.gallery.comic.R;

public class AbsTabActivity extends AbsFragmentActivity{
	protected Map<String,WeakReference<View>> mIndicatorMap=new HashMap();
	protected LayoutInflater mInflater;
	protected TabHost mTabHost;
	protected final AbsTabActivity.TabManager.TabSwitchListener
		mTabListener=new AbsTabActivity.TabManager.TabSwitchListener(){
		public void onTabSwitched(String str,Fragment f1,Fragment f2){
			AbsTabActivity.this.handleTabSwitch(str,f1,f2);
		}
	};
	protected TabManager mTabManager;
	protected TabWidget mTabWidget;
	
	protected View getIndicatorView(String key){
		WeakReference weakReference=(WeakReference)this.mIndicatorMap.get(key);
		if(weakReference==null){
			return null;
		}
		return (View)weakReference.get();
	}
	
	protected void handleTabSwitch(String str,Fragment f1,Fragment f2){
		if(f2!=null&&(f2 instanceof ITabFragment)){
			((ITabFragment)f2).onTabUnSelected();
		}
		if(f1!=null&&(f1 instanceof ITabFragment)){
			((ITabFragment)f1).onTabUnSelected();
		}
	}
	
	protected void initTab(Bundle bundle,List<TabEntry> list){
		this.mInflater=LayoutInflater.from(this);
		this.mTabHost=((TabHost)findViewById(android.R.id.tabhost));
		this.mTabHost.setup();
		this.mTabWidget=((TabWidget)findViewById(android.R.id.tabs));
		//this.mTabManager=new TabManager(this,this.mTabHost,R.id.realtabcontent,this.mTabListener);
		String str=null;
		if(bundle!=null){
			str=bundle.getString("tab");
		}
		int i=0;
		Iterator iterator=list.iterator();
		while(iterator.hasNext()){
			TabEntry tabEntry=(TabEntry)iterator.next();
			TabHost.TabSpec tabSpec=this.mTabHost.newTabSpec(tabEntry.tag);
			View view=makeIndicator(tabEntry.title,tabEntry.icon);
			tabSpec.setIndicator(view);
			this.mTabManager.addTab(tabSpec,tabEntry.clz,tabEntry.args);
			this.mIndicatorMap.put(tabEntry.tag, new WeakReference(view));
			if(str!=null&&str.equals(tabEntry.tag)){
				i=1;
			}
		}
		if(i!=0){
			this.mTabHost.setCurrentTabByTag(str);
		}
	}
	
	protected View makeIndicator(int titleId,int iconId){
		String str=getString(titleId);
		View view=this.mInflater.inflate(R.layout.tab_indicator, this.mTabWidget, false);
		view.setBackgroundResource(R.drawable.tab_indicator);
		((TextView)view.findViewById(R.id.indicator_title)).setText(str);
		((ImageView)view.findViewById(R.id.indicator_icon)).setImageResource(iconId);
		return view;
	}
	
	protected void onSaveInstanceState(Bundle bundle){
		super.onSaveInstanceState(bundle);
		if(this.mTabHost!=null){
			bundle.putString("tab", this.mTabHost.getCurrentTabTag());
		}
	}

	public static class TabEntry{
		public final Bundle args;
		public final Class<? extends Fragment> clz;
		public final int icon;
		public final String tag;
		public final int title;
		
		public TabEntry(String tag,int title,int icon,
				Class<? extends Fragment> clz,Bundle args){
			this.tag=tag;
			this.title=title;
			this.icon=icon;
			this.clz=clz;
			this.args=args;
		}
	}
	
	public static class TabManager implements TabHost.OnTabChangeListener{
		private final FragmentActivity mActivity;
		private final int mContainerId;
		TabInfo mLastTab;
		final TabSwitchListener mListener;
		private final TabHost mTabHost;
		private final HashMap<String,TabInfo> mTabs=new HashMap();
		public TabManager(FragmentActivity activity,TabHost tabHost,
				int containerId,TabSwitchListener listener){
			this.mActivity=activity;
			this.mTabHost=tabHost;
			this.mContainerId=containerId;
			this.mListener=listener;
			this.mTabHost.setOnTabChangedListener(this);
		}
		

		public void addTab(TabHost.TabSpec tabSpec,Class<?> clazz,Bundle bundle){
			tabSpec.setContent(new DummyTabFactory(this.mActivity));
			String str=tabSpec.getTag();
			TabInfo tabInfo=new TabInfo(str,clazz,bundle);
			//TabInfo.access$002(tabInfo,this.mActivity.getSupportFragmentManager().findFragmentByTag(str));
			if(tabInfo.fragment!=null&&!tabInfo.fragment.isDetached()){
				FragmentTransaction fragmentTransaction=this.mActivity.getSupportFragmentManager().beginTransaction();
				fragmentTransaction.detach(tabInfo.fragment);
				fragmentTransaction.commit();
			}
			this.mTabs.put(str,tabInfo);
			this.mTabHost.addTab(tabSpec);
		}
		
		public Fragment getCurrentFragment(){
			if(this.mLastTab!=null){
				return this.mLastTab.fragment;
			}
			return null;
		}
		
		public void onTabChanged(String str){
			TabInfo tabInfo1=(TabInfo)this.mTabs.get(str);
			FragmentTransaction fragmentTransaction;
			Fragment f1;
			Fragment f2;
			if(this.mLastTab!=tabInfo1){
				fragmentTransaction=this.mActivity.getSupportFragmentManager().beginTransaction();
				TabInfo tabInfo2=this.mLastTab;
				f1=null;
				/*if(f2!=null){
					if(this.mLastTab.fragment!=null){
						fragmentTransaction.hide(this.mLastTab.fragment);
					}
					f1=this.mLastTab.fragment;
				}*/
				f2=null;
				if(tabInfo1!=null){
					if(tabInfo1.fragment!=null){
						if(tabInfo1.fragment.isDetached()){
							fragmentTransaction.attach(tabInfo1.fragment);
						}
						fragmentTransaction.show(tabInfo1.fragment);
					}
					//TabInfo.access$002(tabInfo1,Fragment.instantiate(this.mActivity,tabInfo1.clss.getName(),tabInfo1.args));
					fragmentTransaction.add(this.mContainerId,tabInfo1.fragment,tabInfo1.tag);
				}
			}
			f2=tabInfo1.fragment;
			this.mLastTab=tabInfo1;
			//fragmentTransaction.commit();
			this.mActivity.getSupportFragmentManager().executePendingTransactions();
			/*if((this.mListener!=null)&&(f2!=f1)){
				this.mListener.onTabSwitched(str,f2,f1);
			}*/
		}
		
		static class DummyTabFactory implements TabHost.TabContentFactory{
			private final Context mContext;
			
			public DummyTabFactory(Context context){
				this.mContext=context;
			}
			
			public View createTabContent(String str){
				View view=new View(this.mContext);
				view.setMinimumWidth(0);
				view.setMinimumHeight(0);
				return view;
			}
		}
		
		static final class TabInfo{
			private final Bundle args;
			private final Class<?> clss;
			private Fragment fragment;
			private final String tag;
			
			TabInfo(String tag,Class<?> clss,Bundle args){
				this.tag=tag;
				this.clss=clss;
				this.args=args;
			}
		}
		
		static abstract interface TabSwitchListener{
			public abstract void onTabSwitched(String str,Fragment f1,Fragment f2);
		}
	}
}
